/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MA_VA_Hinzufügen.java
 *
 * Created on 17.03.2011, 10:11:39
 */
package bzr_veranstaltungverwaltung;

import java.sql.*;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;

/**
 *
 * @author fhiesl
 */
public class bzr_MA_VA_Hinzufügen extends javax.swing.JFrame {

    /** Creates new form MA_VA_Hinzufügen */
    public Connection con;
    public LinkedList<Mitarbeiter> ll_ma;
    public LinkedList<Mitarbeiter> ll_ausgewaehlt;
    DefaultListModel dlm_ll_ma;
    DefaultListModel dlm_ll_ausgewaehlt;
    DefaultComboBoxModel dcbm;

    public bzr_MA_VA_Hinzufügen() throws ClassNotFoundException, InstantiationException, IllegalAccessException {

        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            String url = "jdbc:mysql://mysql.bezirksrundschau.com:3306/brdbm_1";
            con = DriverManager.getConnection(url, "brdbm", "br2011!db");
            System.out.println("Verbunden");




        } catch (SQLException ex) {
            Logger.getLogger(bzr_MA_VA_Hinzufügen.class.getName()).log(Level.SEVERE, null, ex);
        }
        comboBoxInit();
        initComponents();
//        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
//        this.setSize(screen);
        ll_ma = new LinkedList<Mitarbeiter>();
        ll_ausgewaehlt = new LinkedList<Mitarbeiter>();
        dlm_ll_ma = new DefaultListModel();
        dlm_ll_ausgewaehlt = new DefaultListModel();
        jListPromosAufbauer.setModel(dlm_ll_ma);
        jListAusgewähltePromoAufbauer.setModel(dlm_ll_ausgewaehlt);
        listeladen();


    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        jComboBoxAuswahlPromoAufbauer = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        jListPromosAufbauer = new javax.swing.JList();
        jScrollPane2 = new javax.swing.JScrollPane();
        jListAusgewähltePromoAufbauer = new javax.swing.JList();
        jButtonVerschieben = new javax.swing.JButton();
        jButton_löschenList = new javax.swing.JButton();
        jButton_list_speichern = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel_aufbauer = new javax.swing.JLabel();
        jLabel_promos = new javax.swing.JLabel();
        jComboBox_VA = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jComboBoxAuswahlPromoAufbauer.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Promo", "Aufbauer" }));
        jComboBoxAuswahlPromoAufbauer.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jComboBoxAuswahlPromoAufbauerItemStateChanged(evt);
            }
        });

        jListPromosAufbauer.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jListPromosAufbauer.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, jComboBoxAuswahlPromoAufbauer, org.jdesktop.beansbinding.ObjectProperty.create(), jListPromosAufbauer, org.jdesktop.beansbinding.BeanProperty.create("elements"));
        bindingGroup.addBinding(binding);

        jScrollPane1.setViewportView(jListPromosAufbauer);

        jListAusgewähltePromoAufbauer.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jListAusgewähltePromoAufbauer.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane2.setViewportView(jListAusgewähltePromoAufbauer);

        jButtonVerschieben.setText(">>");
        jButtonVerschieben.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonVerschiebenActionPerformed(evt);
            }
        });

        jButton_löschenList.setText("Löschen");
        jButton_löschenList.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_löschenListActionPerformed(evt);
            }
        });

        jButton_list_speichern.setText("Speichern");
        jButton_list_speichern.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_list_speichernActionPerformed(evt);
            }
        });

        jLabel1.setText("Aufbauer:");

        jLabel2.setText("Promos:");

        jLabel_aufbauer.setText("0");

        jLabel_promos.setText("0");

        jComboBox_VA.setModel(dcbm);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jComboBoxAuswahlPromoAufbauer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jButtonVerschieben)))
                        .addGap(14, 14, 14)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButton_löschenList)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(30, 30, 30)
                                        .addComponent(jComboBox_VA, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(layout.createSequentialGroup()
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(jButton_list_speichern, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE))))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(224, 224, 224)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(10, 10, 10)
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel_promos, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel_aufbauer, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap(48, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboBoxAuswahlPromoAufbauer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton_löschenList))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jComboBox_VA, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(41, 41, 41)
                                .addComponent(jButtonVerschieben))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(49, 49, 49)
                                .addComponent(jButton_list_speichern, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jScrollPane2)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 230, Short.MAX_VALUE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel_aufbauer))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel_promos))
                .addContainerGap(23, Short.MAX_VALUE))
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jComboBoxAuswahlPromoAufbauerItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jComboBoxAuswahlPromoAufbauerItemStateChanged
        if (jComboBoxAuswahlPromoAufbauer.getSelectedIndex() == 0) {
            listePromosAufbauerWechseln("Promo");


        }
        if (jComboBoxAuswahlPromoAufbauer.getSelectedIndex() == 1) {
            listePromosAufbauerWechseln("Aufbauer");


        }


    }//GEN-LAST:event_jComboBoxAuswahlPromoAufbauerItemStateChanged

    private void jButtonVerschiebenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonVerschiebenActionPerformed
        // TODO add your handling code here:
        int index = jListPromosAufbauer.getSelectedIndex();
        Mitarbeiter ma = (Mitarbeiter) dlm_ll_ma.getElementAt(index);

        if(dlm_ll_ausgewaehlt.contains(ma)==false){
            dlm_ll_ausgewaehlt.addElement(ma);
        ll_ausgewaehlt.add(ma);
        if (ma.getMaart().equals("Promo")) {
            jLabel_promos.setText("" + (Integer.parseInt(jLabel_promos.getText()) + 1));
            
        if (dlm_ll_ausgewaehlt.contains(ma) == false) {
            dlm_ll_ausgewaehlt.addElement(ma);
            ll_ausgewaehlt.add(ma);
            if (ma.getMaart().equals("Promo")) {
                jLabel_promos.setText("" + (Integer.parseInt(jLabel_promos.getText()) + 1));
            } else {
                jLabel_aufbauer.setText("" + (Integer.parseInt(jLabel_aufbauer.getText()) + 1));
            }
            }

        }
        } else {
            JOptionPane.showMessageDialog(this, "Der Mitarbeiter ist bereits ausgewählt !!!");
        }
            
        
    }//GEN-LAST:event_jButtonVerschiebenActionPerformed

    private void jButton_löschenListActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_löschenListActionPerformed
        // TODO add your handling code here:
        if (jListAusgewähltePromoAufbauer.getSelectedIndex() >= 0) {
            Mitarbeiter m = (Mitarbeiter) jListAusgewähltePromoAufbauer.getSelectedValue();
            dlm_ll_ausgewaehlt.removeElement(m);
            ll_ausgewaehlt.remove(m);
            if (m.isMaart("Promo")) {
                jLabel_promos.setText("" + (Integer.parseInt(jLabel_promos.getText()) - 1));
            }
            if (m.isMaart("Aufbauer")) {
                jLabel_aufbauer.setText("" + (Integer.parseInt(jLabel_aufbauer.getText()) - 1));
            }
        } else {
            JOptionPane.showMessageDialog(this, "Sie müssen zuerst einen Mitarbeiter auswählen bevor sie ihn löschen");
        }

    }//GEN-LAST:event_jButton_löschenListActionPerformed
    private void jButton_list_speichernActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_list_speichernActionPerformed
        try {
            // TODO add your handling code here:
            Statement st = con.createStatement();
            Iterator i = ll_ausgewaehlt.iterator();
            while(i.hasNext()){
                Mitarbeiter ma =(Mitarbeiter) i.next();
                Veranstaltungen va = (Veranstaltungen) jComboBox_VA.getSelectedItem();
                st.executeUpdate("INSERT INTO VA_MA "
                        + "VALUES ("+va.getV_id()+","+ma.getMa_id()+")");
            }
            dlm_ll_ausgewaehlt.clear();
            ll_ausgewaehlt.clear();
            jLabel_aufbauer.setText("0");
            jLabel_promos.setText("0");

        } catch (SQLException ex) {
            Logger.getLogger(bzr_MA_VA_Hinzufügen.class.getName()).log(Level.SEVERE, null, ex);
        }

    }//GEN-LAST:event_jButton_list_speichernActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                try {
                    new bzr_MA_VA_Hinzufügen().setVisible(true);



                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(bzr_MA_VA_Hinzufügen.class.getName()).log(Level.SEVERE, null, ex);
                } catch (InstantiationException ex) {
                    Logger.getLogger(bzr_MA_VA_Hinzufügen.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IllegalAccessException ex) {
                    Logger.getLogger(bzr_MA_VA_Hinzufügen.class.getName()).log(Level.SEVERE, null, ex);
                }


            }
        });


    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonVerschieben;
    private javax.swing.JButton jButton_list_speichern;
    private javax.swing.JButton jButton_löschenList;
    private javax.swing.JComboBox jComboBoxAuswahlPromoAufbauer;
    private javax.swing.JComboBox jComboBox_VA;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel_aufbauer;
    private javax.swing.JLabel jLabel_promos;
    private javax.swing.JList jListAusgewähltePromoAufbauer;
    private javax.swing.JList jListPromosAufbauer;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

    private void listeladen() {
        try {

            Statement st = (Statement) con.createStatement();
            ResultSet rs = st.executeQuery("SELECT * FROM Freie_Mitarbeiter");


            while (rs.next()) {
                Mitarbeiter ma = new Mitarbeiter();
                ma.setMa_id(rs.getInt("Mitarbeiter_id"));
                ma.setVorname(rs.getString("Vorname"));
                ma.setNachname(rs.getString("Nachname"));
                ma.setTelefonnummer((rs.getInt("Telefonnummer")));
                ma.setEmail(rs.getString("Email"));
                ma.setBezirk(rs.getString("Bezirk"));
                ma.setGebDatum(rs.getDate("GebDatum"));
                ma.setAusbildung(rs.getString("Ausbildung"));
//                ma.setKonfektionsgröße(rs.getInt("Konfektionsgroeße"));
                ma.setFuehrerschein(rs.getString("Fuehrerschein"));
                ma.setSvnr(rs.getInt("SVNr"));
                ma.setStaatsbuergerschaft(rs.getString("Staatsbuergerschaft"));
                ma.setBankverbindung(rs.getInt("Bankverbindung"));
                ma.setMaart(rs.getString("MAArt"));
                ll_ma.add(ma);


            }

            listePromosAufbauerWechseln("Promo");





        } catch (SQLException ex) {
            Logger.getLogger(bzr_MA_VA_Hinzufügen.class.getName()).log(Level.SEVERE, null, ex);
        }


    }

    private void listePromosAufbauerWechseln(String art) {
        dlm_ll_ma.clear();
        Iterator i = ll_ma.iterator();


        if (art.equals("Promo")) {
            while (i.hasNext()) {
                Mitarbeiter ma = (Mitarbeiter) i.next();


                if (ma.getMaart().equals("Promo")) {
                    dlm_ll_ma.addElement(ma);


                }
            }
        }
        if (art.equals("Aufbauer")) {
            while (i.hasNext()) {
                Mitarbeiter ma = (Mitarbeiter) i.next();


                if (ma.getMaart().equals("Aufbauer")) {
                    dlm_ll_ma.addElement(ma);

                }
            }
        }
    }

    private void comboBoxInit() {
        try {
            dcbm = new DefaultComboBoxModel();
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery("SELECT * FROM Veranstaltungen");
            while(rs.next()){
                Veranstaltungen va = new Veranstaltungen();
                va.setV_id(rs.getInt("Veranstaltungs_id"));
                va.setName(rs.getString("Name"));
                va.setOrt(rs.getString("Ort"));
                va.setPlz(rs.getInt("PLZ"));
                va.setStraße(rs.getString("Strasse"));
                va.setHausnr(rs.getInt("HausNr"));
                va.setStartdatum(rs.getDate("Startdatum"));
                va.setStartdatum(rs.getDate("Enddatum"));
                va.setZustaendigeGeschaeftsstelle(rs.getString("ZustaendigeGeschaeftsstelle"));
                dcbm.addElement(va);
            }
        } catch (SQLException ex) {
            Logger.getLogger(bzr_MA_VA_Hinzufügen.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}
